﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MediaRecommender.Controller;
using MediaRecommender.Model;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Associated_People_Controller_Determine_Person()
        {
            AssociatedPeopleController apc = new AssociatedPeopleController();
            Assert.IsTrue(apc.CreateAssociatedPerson<Director>("test director") is Director);
            Assert.IsTrue(apc.CreateAssociatedPerson<Actor>("test actor") is Actor);
        }

        [TestMethod]
        public void Associated_People_Controller_Determine_Same_Person()
        {
            AssociatedPeopleController apc = new AssociatedPeopleController();
            Director test1 = apc.CreateAssociatedPerson<Director>("test");
            Actor test2 = apc.CreateAssociatedPerson<Actor>("test");

            Assert.IsTrue(test1.ID == test2.ID);
        }

        [TestMethod]
        public void Login_Controller()
        {
            string userName = "boge";
            string password = "123456";

            UserCollection.Instance.Add(new User(userName, password, DateTime.Now, Sex.Female, new List<int>()));

            LoginController LC = new LoginController();

            Assert.IsTrue(LC.Login(userName, password));
            Assert.IsFalse(LC.Login("IDoNotExist", "123456"));
            Assert.IsFalse(LC.Login(userName, "ThisIsNotMyPassword"));
        }

        [TestMethod]
        public void Media_Controller_Book()
        {
            string title = "hey with you book";
            MediaController MC = new MediaController();
            MC.CreateMedia(new List<Author>(), title, new List<int>(), "cover");
            Assert.IsTrue(MediaCollection.Instance.GetMedia(x => x.Title.Equals(title)).Count == 1);
            Assert.IsTrue(MediaCollection.Instance.GetMedia(x => x.Title.Equals("is not correct")).Count == 0);

        }

        [TestMethod]
        public void Media_Controller_VideoGame()
        {
            string title = "hey with you game";
            MediaController MC = new MediaController();
            AssociatedPeopleController AC = new AssociatedPeopleController();
            MC.CreateMedia(AC.CreateAssociatedPerson<GameStudio>("FancyPantsStudio"), AC.CreateAssociatedPerson<Publisher>("GIEEEFMEMONNEYH"), new List<int>(), title, new List<int>(), "cover");
            Assert.IsTrue(MediaCollection.Instance.GetMedia(x => x.Title.Equals(title)).Count == 1);
            Assert.IsTrue(MediaCollection.Instance.GetMedia(x => x.Title.Equals("is not correct")).Count == 0);

        }

        [TestMethod]
        public void Media_Controller_Movie()
        {
            string title = "hey with you movie";
            MediaController MC = new MediaController();
            AssociatedPeopleController AC = new AssociatedPeopleController();
            MC.CreateMedia(AC.CreateAssociatedPerson<Director>("Joe"), new List<Actor>(), title, new List<int>(), "cover");
            Assert.IsTrue(MediaCollection.Instance.GetMedia(x => x.Title.Equals(title)).Count == 1);
            Assert.IsTrue(MediaCollection.Instance.GetMedia(x => x.Title.Equals("is not correct")).Count == 0);

        }

        [TestMethod]
        public void Check_Adapt_Vector()
        {
            string title = "SomeCleverTitle";
            List<int> genres = new List<int>();
            genres.Add(1);
            List<Author> authors = new List<Author>();
            authors.Add(new Author("Hans", 2));
            MediaController MC = new MediaController();
            bool book = MC.CreateMedia(authors, title, genres, "");

            Assert.IsTrue(MediaCollection.Instance.GetMedia(x => x.Title.Equals(title))[0].VectorLength == 2);
            MediaCollection.Instance.GetMedia(x => x.Title.Equals(title))[0].Genres.Add(4);
            Assert.IsFalse(MediaCollection.Instance.GetMedia(x => x.Title.Equals(title))[0].VectorLength == 3);
        }

        [TestMethod]
        public void Add_Media()
        {
            MediaController MC = new MediaController();
            UserRegistrationController URC = new UserRegistrationController();
            URC.CheckIfUserIsValid("joe", "123456", DateTime.Now, Sex.Female, new List<int>());
            MedialistController MLC = new MedialistController(UserCollection.Instance.GetUser("joe"));
            MC.CreateMedia(new List<Author>(), "CleverTitle", new List<int>(), "");
            MLC.AddMedia(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0], 7);

            int raitngOut = 0;
            UserCollection.Instance.GetUser("joe").MediaList.TryGetValue(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0], out raitngOut);
            Assert.IsTrue(raitngOut == 7);
            MLC.AddMedia(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0], 9);

            UserCollection.Instance.GetUser("joe").MediaList.TryGetValue(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0], out raitngOut);
            Assert.IsTrue(raitngOut == 9);
        }

        [TestMethod]
        public void Remove_Media()
        {
            MediaController MC = new MediaController();
            UserRegistrationController URC = new UserRegistrationController();
            MedialistController MLC = new MedialistController(UserCollection.Instance.GetUser("joe"));
            MC.CreateMedia(new List<Author>(), "CleverTitle", new List<int>(), "");
            MLC.AddMedia(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0], 7);

            int ratingOut = 0;
            Assert.IsTrue(UserCollection.Instance.GetUser("joe").MediaList.TryGetValue(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0], out ratingOut));
            MLC.RemoveMedia(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0]);

            Assert.IsFalse(UserCollection.Instance.GetUser("joe").MediaList.TryGetValue(MediaCollection.Instance.GetMedia(x => x.Title.Equals("CleverTitle"))[0], out ratingOut));


            
        }

        [TestMethod]
        public void User_Reg_Controller()
        {
            UserRegistrationController URC = new UserRegistrationController();
            URC.CheckIfUserIsValid("Harald", "123456", DateTime.Now, Sex.Female, new List<int>());

            Assert.IsNotNull(UserCollection.Instance.GetUser("Harald"));

            try
            {
                URC.CheckIfUserIsValid("qwertyuiopasdfgh", "123456", DateTime.Now, Sex.Female, new List<int>());
                Assert.IsTrue(false);
            }
            catch (ArgumentException e) { Assert.IsTrue(true); }

            try
            {
                URC.CheckIfUserIsValid("Hans", "1234", DateTime.Now, Sex.Female, new List<int>());
                Assert.IsTrue(false);
            }
            catch (ArgumentException e) { Assert.IsTrue(true); }

            try
            {
                URC.CheckIfUserIsValid("Harald", "123456", DateTime.Now, Sex.Female, new List<int>());
                Assert.IsTrue(false);
            }
            catch (ArgumentException e) { Assert.IsTrue(true); }
        }
    }
}
